package 树;
/**
 * https://leetcode-cn.com/leetbook/read/data-structure-binary-tree/xoh1zg/
 */
public class 二叉树的最大深度 {
    public class TreeNode {
       int val;
       TreeNode left;
       TreeNode right;
       TreeNode() {}
       TreeNode(int val) { this.val = val; }
       TreeNode(int val, TreeNode left, TreeNode right) {
           this.val = val;
           this.left = left;
           this.right = right;
       }
    }

    //函数定义，给定一个根节点为root的二叉树，返回它的最大深度
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;

        int leftDepth = maxDepth(root.left); //计算左子树的最大深度
        int rightDepth = maxDepth(root.right); //计算右子树的最大深度

        return 1 + Math.max(leftDepth,rightDepth);
    }
}
